﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using TSites.Xml;
using TSites.Config;

namespace TSites.Language
{
    public class TsMessageUtil
    {
         private static TsMessageUtil _instance;
        private static TsXmlEngine _xmlEngine;
        private static Dictionary<string, Object> _laguage;

        /// <summary>
        /// Multilanguage resource
        /// </summary>
        private TsMessageUtil() { }// Constructor

        public static TsMessageUtil GetInstance()
        {
            if (_instance == null)
            {
                _instance = new TsMessageUtil();
                _instance.Load();
            }            
            return _instance;
        }

        private void Load()
        {
            _laguage = new Dictionary<string, Object>();
            TsConfig config = TsConfig.GetInstance();
            string messagePath = config.MessagePath;
            string[] filePaths = Directory.GetFiles(messagePath, "*.txt");
            FileInfo file;
            if (filePaths != null)
            {
                for (int i = 0; i < filePaths.Length; i++)
                {
                    _xmlEngine = new TsXmlEngine(filePaths[i]);
                    file = new FileInfo(filePaths[i]);
                    _laguage.Add(file.Name.Substring(0, file.Name.Length - file.Extension.Length), _xmlEngine);//.txt = 4
                }
            }           
        }

        public String GetMessage(String messageId,string[] parameter, string languageType = null)
        {
            string result="";
            string value = "";

            TsConfig config = TsConfig.GetInstance();
            if (languageType == null || languageType == "")
                languageType = config.DefaultLanguage;
            if (languageType != null)
            {
                if (_laguage.ContainsKey(languageType))
                {
                    TsXmlEngine reader = (TsXmlEngine)_laguage[languageType];
                    if (reader != null)
                        value = reader.GetContent("msgId", messageId);
                }                
            }
            if (value != null && parameter != null)
            {                
                string[] parts = value.Split(new string[] { config.ParamPattern }, StringSplitOptions.None);
                if (parts != null && parts.Length > 1)
                {
                    int i = 0;
                    while (i < parts.Length)
                    {
                        if (i < parameter.Length && i < parts.Length - 1)
                            result = result + parts[i] + parameter[i];
                        else if (i == parts.Length - 1)
                            result = result + parts[i];
                        else
                            result = result + parts[i] + config.ParamPattern;
                        i++;
                    }
                }
                else
                    result = value;
            }
            else
                result = value;
            return result;
        }
    }
}
